﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using GtsServer.Manage.Controller;
using GtsServer.Model;
using GtsServer.Common;

namespace GtsServer.Manage.IISPage.Ajax
{
    /// <summary>
    /// TaskConditionAjax 的摘要说明
    /// </summary>
    public class TaskConditionAjax : IHttpHandler
    {
        TaskConditionController controller = new TaskConditionController();
        public void ProcessRequest(HttpContext context)
        {
            string type = string.Empty;
            if (context.Request["action"] != null)
            {
                type = context.Request["action"];

                switch (type)
                {
                    case "GetList":
                        GetList(context);
                        break;
                    case "Add":
                        Add(context);
                        break;
                    case "Update":
                        Update(context);
                        break;
                    case "Delete":
                        Delete(context);
                        break;
                    case "GetConditionMould":
                        GetConditionMould(context);
                        break;
                    default:
                        break;
                }
            }
        }

        private void GetConditionMould(HttpContext context)
        {
            List<TaskConditionMouldModel> list = new List<TaskConditionMouldModel>();
            foreach (var item in Enum.GetValues(typeof(EnumTaskConditionMould)))
            {
                TaskConditionMouldModel model = new TaskConditionMouldModel()
                {
                    name = item.ToString(),
                    value = Convert.ToInt32(item)
                };
                list.Add(model);
            }
            context.Response.Write(JsonHelper.ListToJson<TaskConditionMouldModel>(list));
        }



        private void Delete(HttpContext context)
        {
            int id = Convert.ToInt32(context.Request["id"].ToString());
            context.Response.Write(controller.Delete(id));
        }

        private void Update(HttpContext context)
        {
            int id = Convert.ToInt32(context.Request["id"].ToString());
            int taskID = Convert.ToInt32(context.Request["taskID"].ToString());
            int prizeID = Convert.ToInt32(context.Request["prizeID"].ToString());
            int ChanceMouldID = Convert.ToInt32(context.Request["ChanceMouldID"].ToString());
            int EventID = Convert.ToInt32(context.Request["EventID"].ToString());
            string value = context.Request["value"];
            int mouldConditionID = Convert.ToInt32(context.Request["mouldConditionID"].ToString());

            TaskCondition model = new TaskCondition()
            {
                ID = id,
                PrizeID = prizeID,
                ConditionValue = value,
                TaskID = taskID,
                ChanceMouldID = ChanceMouldID,
                EventID = EventID,
                TaskMouldConditionID = mouldConditionID
            };
            context.Response.Write(controller.Update(model));
        }

        private void Add(HttpContext context)
        {
            int taskID = Convert.ToInt32(context.Request["taskID"].ToString());
            int prizeID = Convert.ToInt32(context.Request["prizeID"].ToString());
            int ChanceMouldID = Convert.ToInt32(context.Request["ChanceMouldID"].ToString());
            int EventID = Convert.ToInt32(context.Request["EventID"].ToString());
            string value = context.Request["value"];
            int mouldConditionID = Convert.ToInt32(context.Request["mouldConditionID"].ToString());

            TaskCondition model = new TaskCondition()
            {
                PrizeID = prizeID,
                ConditionValue = value,
                TaskID = taskID,
                ChanceMouldID = ChanceMouldID,
                EventID = EventID,
                TaskMouldConditionID = mouldConditionID
            };
            context.Response.Write(controller.Add(model));
        }

        private void GetList(HttpContext context)
        {
            int taskID = Convert.ToInt32(context.Request["taskID"].ToString());
            string json = string.Empty;

            List<TaskConditionShow> list = controller.GetListAndPrizeName(taskID);
            json = JsonHelper.ListToJson<TaskConditionShow>(list);
            context.Response.Write(json);
        }
        //private void GetListByType(HttpContext context)
        //{
        //    int taskID = Convert.ToInt32(context.Request["taskID"].ToString());
        //    string json = string.Empty;

        //    List<TaskConditionShow> list = controller.GetListAndPrizeName(taskID);
        //    json = JsonHelper.ListToJson<TaskConditionShow>(list);
        //    context.Response.Write(json);
        //}

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}